#pragma once

#include "bondchart.h"
#include "bondcharttools.h"
#include <widgets/chart/basechart_axis.h>
#include <widgets/chart/basechart_layoutitem.h>
#include <QtWidgets/QWidget>
#include <QtWidgets/QVBoxLayout>
#include <qbprotocol/include/SSQBAModel.h>
#include <bondlib/BondInfo.h>

namespace qb
{
	typedef struct _tag_DealRealTime_tmp {//成交实时
		float		m_Price;
		char		m_Type[16];
		char		m_Broker[64];
		time_t		m_time;
		_tag_DealRealTime_tmp() {
			memset(this, 0, sizeof(_tag_DealRealTime_tmp));
		};
		const bool operator < (const _tag_DealRealTime_tmp& obj) const
		{
			return (m_time < obj.m_time);
		}
	}DealRealTime, * PDealRealTime;

	class DealRealTimeList {
	public:
		std::list<DealRealTime> m_List;
	public:
		DealRealTimeList() {};
		void Clear() { m_List.clear(); };
		void Insert(const DealRealTime& item) { m_List.push_back(item); }

		void RemoveSameItem() {
			std::list<DealRealTime>::iterator itExchange1, itExchange2;
			itExchange1 = m_List.begin();
			itExchange2 = itExchange1;
			if (itExchange2 != m_List.end())
				++itExchange2;

			time_t mt1 = -1, mt2 = -1;
			for (; itExchange2 != m_List.end();)
			{
				mt1 = itExchange1->m_time;
				mt2 = itExchange2->m_time;

				if (mt1 == mt2)
				{
					itExchange1 = m_List.erase(itExchange1);
				}
				else
				{
					++itExchange1;
				}
				++itExchange2;
			}
		}
	};

    class BondQuoteDealChart
		: public QWidget
		, public BaseChartCustomAxisGrid
		, public BaseChartCustomTextElement
    {
		Q_OBJECT
    public:
		BondQuoteDealChart(QWidget* parent = nullptr);
		~BondQuoteDealChart();

		void setBondInfo(const CBondInfo* bondinfo);
		void loadKLineData(xQBABrokerDealKlineDayList_c* pData);
		void loadKLineCloseData(xQBABrokerKlineDayCloseList_Ack* pData);
		void loadRealTimeData(xQBMarketStreamList_c* pData);
		void createChart();
		void refreshData();
		void resetData();
		void setSelect(int select);
		int getSelect() { return m_nBtnSelect; }
		void setCalendar(int startDay, int endDay);
		void setMarketType(em_BrokerKinds emCurType);
		void setPreClose(const char* pPreClose);
		void setHotPosSize(int size);
		void setDateSel(int nIndex);
		void setLeftDrag(double ratio);
		void setRightDrag(double ratio);
		void setCentralDrag(double ratio1, double ratio2);
		
		PriceKLineList* GetPriceKLineList();
		DealRealTimeList* GetDealRealTimeList();

		BaseChartDragBarElement* dragBar() { return bcde; }

		// BaseChartCustomAxisGrid
		virtual bool drawGridXLines(BaseChartPainter* painter);
		virtual bool drawGridYLines(BaseChartPainter* painter);

		// BaseChartCustomTextElement
		virtual bool customDraw(BaseChartPainter* painter, const QRect& rect);

	signals:
		void mouseDoubleClick(QWidget*);
		void calendarChanged(int, int);

	protected:
		void inputKLineData(xQBABrokerDealKlineDayList_c* pData);
		void inputKLineCloseData(xQBABrokerKlineDayCloseList_Ack* pData);
		void inputRealTimeData(xQBMarketStreamList_c* pData);
		int getDateSel(int nIndex);
		bool getCalendarSel(int& nBegin, int& nEnd);
		bool isValidCalendar(int& nStartDay, int& nEndDay);
		void GetShowLimit(double fMinXShow, double fMaxXShow, double& fMinYShow, double& fMaxYShow);
		void GetShowLimit1(double fMinXShow, double fMaxXShow);
		void GetShowLimit2(double fMinXShow, double fMaxXShow);
		void drawGridXLines1(BaseChartPainter* painter);
		void drawGridXLines2(BaseChartPainter* painter);
		void drawMouseMoveLine(BaseChartPainter* painter);
		int GetKLineIndex(int nIndex, std::list<PriceKLine>::iterator& it);
		QString	GetKLineDateString(std::list<PriceKLine>::iterator& it);
		QString	strInt2DateInTable(UINT nDate);
		bool IsValidPrice(const char* price) const;
		bool IsValidPrice(double price) const;
		void FormatWholeMktDealPrice(QString& strPrice, const char* cParam);
		bool GetDealRealTimeIndex(int nIndex, std::list<DealRealTime>::iterator& it);
		QString	GetDealRealTimeString(std::list<DealRealTime>::iterator& it);
		QString strChartWeekDay(int i);
		double getXPos_Value(double value);

		void paintEvent(QPaintEvent* event) override;
		void leaveEvent(QEvent* event) override;

		Q_SLOT void onMouseMove(QMouseEvent* event);

	private:
		BaseChartCustomPlot*		customPlot		= nullptr;
		QVBoxLayout*				verticalLayout	= nullptr;
		BaseChartDragBarElement*	bcde			= nullptr;

		CBondInfo*					mBondInfo		= nullptr;
		PriceKLineList				m_DataKLineBroker;//报价，成交日线
		PriceKLineList				m_DataKLineCFETS;
		PriceKLineList				m_DataKLineExchange;
		DealRealTimeList			m_DataDealRealTimeBroker;//实时成交
		DealRealTimeList			m_DataDealRealTimeCFETS;
		DealRealTimeList			m_DataDealRealTimeExchange;

		int							m_nBtnSelect;
		int							m_nMarketType;
		int							m_nHotPoint;
		int							m_nHotPosSize;
		int							m_nDateSel;
		int							m_nStartDay;
		int							m_nEndDay;
		int							m_nBegin;
		int							m_nEnd;
		int							m_nRealBegin;
		int							m_nRealEnd;
		bool						m_bShowLine1;
		bool						m_bShowLine2;
		char						m_szPreClose[32];
		double						m_dMinX;
		double						m_dMaxX;
		double						m_dMinY;
		double						m_dMaxY;
		double						m_dOriginMinX;
		double						m_dOriginMaxX;
		double						m_dDragLeft;
		double						m_dDragRight;
    };
}

